<?php
/**
 * StringHelper.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/10/21- 12:13 下午
 */

namespace common\helpers;


class StringHelper
{

    public static function getCurrentTime()
    {
        list ($msec, $sec) = explode(" ", microtime());
        return (float)$msec + (float)$sec;
    }

    public static function changeToArea($address)
    {
        preg_match('/(.*?(省|自治区|北京市|天津市))/', $address, $matches);
        if (count($matches) > 1) {
            $province = $matches[count($matches) - 2];
            $address = str_replace($province, '', $address);
        }
        preg_match('/(.*?(市|自治州|地区|区划|县))/', $address, $matches);
        if (count($matches) > 1) {
            $city = $matches[count($matches) - 2];
            $address = str_replace($city, '', $address);
        }
        preg_match('/(.*?(区|县|镇|乡|街道))/', $address, $matches);
        if (count($matches) > 1) {
            $area = $matches[count($matches) - 2];
            $address = str_replace($area, '', $address);
        }
        return [
            'province' => isset($province) ? $province : '',
            'city' => isset($city) ? $city : '',
            'area' => isset($area) ? $area : '',
        ];
    }


    /**
     * 数组json
     * @param array $data
     * @return false|string
     */
    public static function jsonEncode(array $data)
    {
        return json_encode($data,JSON_UNESCAPED_UNICODE);
    }


    /**
     * 字符集转utf-8编码
     * @param $str
     * @return string
     */
    public static function toUTF8($str)
    {
        $encode = mb_detect_encoding($str, ["ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5']);
        if (strtoupper($encode) != 'UTF-8') $str = mb_convert_encoding($str, 'utf-8', $encode);
        return $str;
    }


    /**
     *
     * @param string $json
     * @return mixed
     */
    public static function jsonDecode(string $json)
    {
        return json_decode($json,true);
    }


    /**
     * 按gbk编码计算字符串长度
     * @param string $str
     * @return int
     */
    public static function calLenByGBK(string $str):int
    {
        $gbkStr = mb_convert_encoding($str,'GBK','UTF-8');
        return strlen($gbkStr);
    }



    /**
     * 将字符串按照gkb 编码计算方式截取
     * @param string $str
     * @param int $length
     * @param int $type
     * @return bool|false|string|string[]|null
     */
    public static function strCutByGBK(string $str,int $length,int $type =1)
    {
        $str=preg_replace('|[0-9a-zA-Z/]+|','',$str);
        $str=str_replace(' ','',$str);
        $str = preg_replace("/[\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\.|\/|\;|\'|\`|\-|\=|\\\|\|]{1}/",'', $str);
        $gbkStr = mb_convert_encoding($str,'GBK','UTF-8');
        $strArr = str_split($gbkStr);
        $calLen = 0;
        $totalLen = strlen($gbkStr);
        foreach($strArr as $k => $item)
        {
            switch ($type)
            {
                case 2: //从前面截取
                    if(strlen($item) >=2)
                    {
                        $totalLen -=2;
                    }else{
                        $totalLen -=1;
                    }

                    if($totalLen >= $length)
                    {
                        unset($strArr[$k]);
                    }
                    break;
                default:
                    if(strlen($item) >=2)
                    {
                        $calLen +=2;
                    }else{
                        $calLen +=1;
                    }
                    if($calLen >=$length)
                    {
                        unset($strArr[$k]);
                    }
            }
        }
        return mb_convert_encoding(implode('',$strArr),'UTF-8','GBK');
    }

    /**
     * 检查字符串是否为英文
     * @param string $str
     * @return bool
     */
    public static function checkStrIsYw(string $str)
    {
        $strA = trim($str);
        $lenA = strlen($strA);
        $lenB = mb_strlen($strA, "utf-8");
        if ($lenA === $lenB) {
            return true; //全英文
        }
        return false;
    }


    /**
     * 获取随机字符串
     * @param int $length
     * @return string
     */
    public static function getRandomStr(int $length)
    {
        $token = "";
        $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $codeAlphabet.= "0123456789";


        for($i=0;$i<$length;$i++){
            $token .= $codeAlphabet[mt_rand(0,strlen($codeAlphabet)-1)];
        }
        return $token;
    }

    /**
     * 使用特殊字符串隔开
     * @param string $string
     * @return array|false|string[]
     */
    public static function cutByString(string $string)
    {
        return preg_split("/(#|:|;|,|，|、)/",$string);
    }
}
